package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.AgencyProductRelationshipsDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.AgencyProductRelationshipsMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class AgencyProductRelationshipsDAO extends  BaseDAO<AgencyProductRelationshipsDO>{
    @Resource
    private AgencyProductRelationshipsMapper agencyProductRelationshipsMapper;

    @PostConstruct
    public void init(){
        this.mapper = agencyProductRelationshipsMapper;
    }

    public IPage<AgencyProductRelationshipsDO> selectByPageName(Pagination pagination, String username) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("product_state","审核中").or().eq("product_state","已上架").or().eq("product_state","上架失败");
        return mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<AgencyProductRelationshipsDO> selectByPageCategory(Pagination pagination, String state,String username) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("product_state",state);
        wrapper.eq("username",username);
        return mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public List<AgencyProductRelationshipsDO> selectByUsernameAndEntName(String username, String entName) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        wrapper.eq("ent_name",entName);
        return mapper.selectList(wrapper);
    }

    public List<AgencyProductRelationshipsDO> selectByUserName(String username) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        return mapper.selectList(wrapper);
    }

    public AgencyProductRelationshipsDO selectByProductId(int id) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",id);
        return mapper.selectOne(wrapper);
    }

    public void deleteByEntName(String entName) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("ent_name",entName);
        mapper.delete(wrapper);
    }

    public List<AgencyProductRelationshipsDO> selectByEntName(String entName) {
        QueryWrapper<AgencyProductRelationshipsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("ent_name",entName);
        return mapper.selectList(wrapper);
    }


    public List<AgencyProductRelationshipsDO> selectByHavaAgecy() {
        List<AgencyProductRelationshipsDO> agencyProductRelationshipsDOS=agencyProductRelationshipsMapper.selectByHavaAgecy();
        return  agencyProductRelationshipsDOS;
    }
}
